﻿
using FreeRedis;
using UtilsBase;
using static FreeRedis.RedisClient;

namespace UtilsCache
{ /// <summary>
  ///  支持分布式锁
  /// </summary>
    public class UtilFreeRedis
    {
        static string connstr = UtilAppsettingCache.getSetting("redis:host") + "," + "database=" + UtilAppsettingCache.getSetting("redis:db") + ",password=" + UtilAppsettingCache.getSetting("redis:password");

        public static RedisClient freeRedisClient = new RedisClient(connstr);
        public UtilFreeRedis()
        {
            //try
            //{
            //    if (freeRedisClient == null)
            //    {
            //        freeRedisClient = new RedisClient(connstr);
            //    }
            //}
            //catch { }
            //if (_redisClient == null)
            //{
            //    lock (obj)
            //    {
            //        if (_redisClient == null)
            //        {
            //            _redisClient = new RedisClient($"{_redisOption.RedisHost}:{_redisOption.RedisPort},password={_redisOption.RedisPassword},defaultDatabase={_redisOption.DefaultIndex},poolsize={_redisOption.Poolsize},ssl=false,writeBuffer=10240,prefix={_redisOption.Prefix},asyncPipeline={_redisOption.asyncPipeline},connectTimeout={_redisOption.ConnectTimeout},abortConnect=false");
            //            //设置客户端缓存
            //            if (_redisOption.UseClientSideCache)
            //            {
            //                if (_redisOption.ClientSideCacheKeyFilter == null)
            //                {
            //                    throw new NullReferenceException("如果开启客户端缓存，必须设置客户端缓存Key过滤条件");
            //                }
            //                _redisClient.UseClientSideCaching(new ClientSideCachingOptions()
            //                {
            //                    Capacity = 0,  //本地缓存的容量，0不限制
            //                    KeyFilter = _redisOption.ClientSideCacheKeyFilter,  //过滤哪些键能被本地缓存
            //                    CheckExpired = (key, dt) => DateTime.Now.Subtract(dt) > TimeSpan.FromSeconds(3)  //检查长期未使用的缓存
            //                });
            //            }
            //            return true;
            //        }
            //    }
            //}
            //return _redisClient != null;
            //if (freeRedisClient == null)
            //{
            //    string connstr = UtilsAppsetting.GetSettings("redis:host") + "," + "database=" + UtilsAppsetting.GetSettings("redis:db") + ",password=" + UtilsAppsetting.GetSettings("redis:password");
            //    freeRedisClient = new RedisClient(connstr);
            //}
            // return freeRedisClient;
        }

        /// <summary>
        ///  开启分布式锁，若超时返回null,
        /// </summary>
        /// <param name="key">锁名称</param>
        /// <param name="seconds">等待时间（秒），如设60，在5秒内解锁，就5等5秒再执行下一步，如等到60还没解锁，就放弃操作</param>
        /// <param name="autoDelay">自动延长锁超时时间，看门狗线程的超时时间为timeoutSeconds/2 ， 在看门狗线程超时时间时自动延长锁的时间为timeoutSeconds。除非程序意外退出，否则永不超时。</param>
        /// <returns></returns>

        public static LockController getLock(string key, int seconds = 60, bool autoDelay = true)
        {

            return freeRedisClient.Lock(key, seconds, autoDelay);
        }

        public static void del(string key)
        {
            freeRedisClient.Del(key);
        }
    }
}




